/*--------------------------------
 * Web RPC Services for PspyGear
 *
 * Wraps HTTP Post to simplify
 * calls to PspyGear servers.
 *
 */
'use strict'

//--------------------------------------
//        Web RPC Service
//--------------------------------------
// Create as service which can be used
// to post Action Requests back to the
// PspyGear server. 
var webSvc = angular.module('WebServices', [ ]);
webSvc.factory('RPC', function($http,$q) {
     // build WebRPC object
     return {    
    
        //---------------------------------
        // POST data to url and return a
        // promise result
        fire: function(url, params) {
          
            // prep for async return
            var dfr = $q.defer();
               
            // send request and set callbacks
            // to use the deferred  
            $http({url: url, method: 'POST', data: params })
                .success(function(data,status){
                    dfr.resolve(data);
                })
                .error(function(data,status){
                    // server call failed
                    dfr.reject({result: data, status: status });
                });
              
            // send results via promise 
            return dfr.promise;
        }    
    }
});